import {createRouter, createWebHashHistory} from 'vue-router'
import type {RouteRecordRaw} from 'vue-router'
import {getToken} from "@/utils/user-utils"

const routes: RouteRecordRaw[] = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login.vue')
    },
    {
        path: '/',
        name: 'home',
        component: () => import('@/views/home.vue'),
        children: [
            {
                path: '/welcome',
                name: 'welcome',
                component: () => import('@/views/welcome.vue')
            },
            {
                path: '/myself',
                name: 'myself',
                component: () => import('@/views/myself.vue')
            },
            {
                path: '/manager',
                name: 'manager',
                children: [
                    {
                        path: '/manager/user',
                        name: 'user',
                        component: () => import('@/views/manager/userInfo.vue')
                    },
                    {
                        path: '/manager/dict',
                        name: 'dict',
                        component: () => import('@/views/manager/dictInfo.vue')
                    },
                    {
                        path: '/manager/menu',
                        name: 'menu',
                        component: () => import('@/views/manager/menuInfo.vue')
                    },
                    {
                        path: '/manager/log',
                        name: 'log',
                        component: () => import('@/views/manager/logInfo.vue')
                    },
                    {
                        path: '/manager/notification',
                        name: 'notification',
                        component: () => import('@/views/manager/notificationInfo.vue')
                    },
                    {
                        path: '/manager/param',
                        name: 'param',
                        component: () => import('@/views/manager/paramInfo.vue')
                    },
                    {
                        path: '/manager/role',
                        name: 'role',
                        component: () => import('@/views/manager/roleInfo.vue')
                    },

                ]
            },
            {
                path: '/ai',
                name: 'ai',
                children: [
                    {
                        path: '/ai/config',
                        name: 'aiConfig',
                        component: () => import('@/views/ai/aiConfig.vue')
                    },
                    {
                        path: '/ai/question',
                        name: 'aiQuestion',
                        component: () => import('@/views/ai/aiQuestion.vue')
                    }
                ]
            },
            {
                path: '/monitoring',
                name: 'monitoring',
                children: [
                    {
                        path: '/monitoring/server',
                        name: 'serverMonitoring',
                        component: () => import('@/views/monitoring/serverMonitoring.vue')
                    },
                    {
                        path: '/monitoring/log',
                        name: 'logMonitoring',
                        component: () => import('@/views/monitoring/logMonitoring.vue')
                    },
                    {
                        path: '/monitoring/cache',
                        name: 'cacheMonitoring',
                        component: () => import('@/views/monitoring/cacheMonitoring.vue')
                    },
                ]
            },
            {
                name: 'test',
                path: '/test',
                children: [
                    {
                        path: '/test/test1',
                        name: 'test1',
                        component: () => import('@/views/test/test.vue')
                    }
                ]
            }
        ]
    },
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/not-found.vue'),
    },
]

const router = createRouter({
    routes,
    history: createWebHashHistory()
})

router.beforeEach((to) => {
    if (to.path !== '/login') {
        const token = getToken();
        if (token == '') {
            return '/login'
        }
    }
})

export default router
